package cn.lyjuan.android.demofive.system.particular;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

/**
 * 总结：
 * onInterceptTouchEvent 控制事件是否向子控件传递
 * onTouchEvent 控制事件是否向父控件传递
 *
 * 当控件的onInterceptTouchEvent返回true时，即不再向子控件传递onInterceptTouchEvent事件，且执行控件的onTouchevent方法
 * 当控件的onTouchEvent返回true时，即不再向父控件传递onTouchEvent事件，事件过程到此结束
 *
 * Created by chad on 2016/4/16.
 */
public class TouchEventActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        ParentContainer p = new ParentContainer(this);
        ChildContainer c = new ChildContainer(this);

        p.addView(c, lp);
        this.addContentView(p, lp);
    }
}

class ParentContainer extends RelativeLayout
{
    public ParentContainer(Context context)
    {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        boolean result = super.onInterceptTouchEvent(ev);

//        result = true;

        Log.w(ParentContainer.class.getSimpleName(), "onInterceptTourchEvent return >> " + result);

        return result;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        boolean result = super.onTouchEvent(event);

//        result = true;

        Log.w(ParentContainer.class.getSimpleName(), "onTouchEvent return >> " + result);

        return result;
    }
}

class ChildContainer extends FrameLayout
{
    public ChildContainer(Context context)
    {
        super(context);
//
//        Button click = new Button(context);
//        click.setText("测试按钮");
//
//        click.setOnClickListener(new OnClickListener()
//        {
//            @Override
//            public void onClick(View v)
//            {
//                Log.w(ChildContainer.class.getSimpleName(), "response from onclick");
//            }
//        });
//
//        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//
//        this.addView(click, lp);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        boolean result = super.onInterceptTouchEvent(ev);

//        result = true;

        Log.w(ChildContainer.class.getSimpleName(), "onInterceptTourchEvent return >> " + result);

        return result;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        boolean result = super.onTouchEvent(event);

//        result = true;

        Log.w(ChildContainer.class.getSimpleName(), "onTouchEvent return >> " + result);

        return result;
    }
}